package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {
    @DubboReference
    private SetmealService setmealService;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author: 位帅帅
     * @Date: 18:28 2021/1/6
     * @Params: [checkgroupIds, setmeal]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 添加套餐
    */
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @PostMapping("add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        log.info("[套餐-添加]checkgroupIds: {}, setmeal:{}",checkgroupIds,setmeal);
        if (!StringUtils.isEmpty(setmeal.getImg())) {
            String img = setmeal.getImg().replace(qiniuUtils.getUrlPrefix(),"");
            setmeal.setImg(img);
        }
        setmealService.add(checkgroupIds,setmeal);
        redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).remove(setmeal.getImg());
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }

    /**
     * @Author: 位帅帅
     * @Date: 18:29 2021/1/6
     * @Params: [queryPageBean]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 分页查询套餐
    */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[套餐-分页查询]queryPageBean: {}",queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        log.info("[套餐-分页查询]pageResult: {}",pageResult);
        return new Result(true,MessageConst.QUERY_SETMEALLIST_SUCCESS,pageResult);
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:24 2021/1/6
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 删除套餐
    */
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @PostMapping("delete")
    public Result delete(Integer id) {
        log.info("[套餐-删除]id: {}",id);
        setmealService.delete(id);
        return new Result(true,MessageConst.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:49 2021/1/6
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 根据id查询套餐
    */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("[套餐-id查询]id: {}",id);
        Setmeal setmeal = setmealService.findById(id);
        log.info("[套餐-id查询]setmeal: {}",setmeal);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:58 2021/1/6
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 根据套餐id查询检查组id
    */
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @GetMapping("findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id) {
        log.info("[套餐-根据套餐id查询检查组id]id: {}",id);
        List<Integer> checkGroupIds = setmealService.findCheckGroupIdsBySetmealId(id);
        log.info("[套餐-根据套餐id查询检查组id]checkGroupIds: {}",checkGroupIds);
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
    }

    /**
     * @Author: 位帅帅
     * @Date: 21:15 2021/1/6
     * @Params: [checkgroupIds, setmeal]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 编辑套餐
    */
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @PostMapping("edit")
    public Result edit(Integer[] checkgroupIds,@RequestBody Setmeal setmeal) {
        log.info("[套餐-编辑]checkgroupIds: {}, setmeal: {}",checkgroupIds,setmeal);
        if (!StringUtils.isEmpty(setmeal.getImg())) {
            String img = setmeal.getImg().replace(qiniuUtils.getUrlPrefix(), "");
            setmeal.setImg(img);
        }
        setmealService.edit(checkgroupIds,setmeal);
        return new Result(true,MessageConst.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * @Author: 位帅帅
     * @Date: 15:46 2021/1/8
     * @Params: [multipartFile]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 图片上传
    */
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD','SETMEAL_EDIT')")
    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile")MultipartFile multipartFile) {
        log.info("[套餐-图片上传]multipartFile: {}",multipartFile);
        String originalFileName = multipartFile.getOriginalFilename();
        String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-","") + "_" + originalFileName;
        try (InputStream inputStream = multipartFile.getInputStream()) {
            qiniuUtils.upload2Qiniu(inputStream,fileName);
//            try (Jedis jedis = jedisPool.getResource()) {
//                jedis.sadd(RedisConst.SETMEAL_PIC_RESOURCES,fileName);
//            }
            redisTemplate.boundSetOps(RedisConst.SETMEAL_PIC_RESOURCES).add(fileName);
            String imgUrl = qiniuUtils.getUrlPrefix() + fileName;
            log.info("[套餐-图片上传]imgUrl: {}",imgUrl);
            return new Result(true,MessageConst.PIC_UPLOAD_SUCCESS,imgUrl);
        } catch (IOException e) {
            log.error("[套餐-图片上传]e: {}",e.getMessage());
            return new Result(false,MessageConst.PIC_UPLOAD_FAIL);
        }
    }
}
